﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ObviousCode.Common.Utils.Library.Dictionaries;
using ObviousCode.UI.Aspect.PluginLibrary.Interfaces;
using ObviousCode.Aspect.PluginLibrary.Interfaces;
using ObviousCode.UI.Aspect.Persistence.AspectSurface.Loaders;
using ObviousCode.UI.Aspect.Persistence.AspectMenu.Loaders;
using ObviousCode.Common.Utils.Library.Exceptions;
using ObviousCode.Aspect.PluginLibrary.Caches;
using ObviousCode.UI.Aspect.PluginLibrary.Cache.Loaders;
using ObviousCode.Common.Utils.Progress;

namespace ObviousCode.UI.Aspect.Persistence.AspectDesigner.Loaders
{
    public class DesignerLoader_V1 : DesignerLoader
    {
        Dictionary<string, IAspectItem> _cache;

        public DesignerLoader_V1()
        {
            _cache = new Dictionary<string, IAspectItem>();
        }

        public override PropertyArray AllowVersions
        {
            get { return new PropertyArray(); }
        }

        public override int BackwardsCompatTo
        {
            get { return 1; }
        }
        
        public override PropertyArray RejectVersions
        {
            get { return new PropertyArray(); }
        }

        public override int Version
        {
            get { return 1; }
        }

        protected override void PerformLoad(IAspectDesigner parent, PropertyDictionary saveData)
        {
            NotifyStepCommencing("Initialising", "Initialising Load");

            AspectCache.Main.Clear();

            IAspectSurface surface = parent.GetPersistable<IAspectSurface>();
            IAspectMenu menu = parent.GetPersistable<IAspectMenu>();

            AspectCacheLoader cacheLoader = AspectCacheLoader.LatestVersion;
            MenuLoader menuLoader = MenuLoader.LatestVersion;
            SurfaceLoader surfaceLoader = SurfaceLoader.LatestVersion;

            NotifyStepCompleted("Initialising", "Initialising Complete");

            NotifyStepCommencing("Cache Load", "Populating Aspect Cache");
            cacheLoader.Load(AspectCache.Main, saveData.Get<PropertyDictionary>("cache"));
            NotifyStepCompleted("Cache Load", "Aspect Cache Population Complete");

            NotifyStepCommencing("Menu Load", "Loading Menu Items");
            menuLoader.Load(menu, saveData.Get<PropertyDictionary>("tree"));
            NotifyStepCompleted("Menu Load", "Menu Load Complete");

            NotifyStepCommencing("UI Load", "Loading UI Items");
            surfaceLoader.Load(surface, saveData.Get<PropertyDictionary>("surface"));
            NotifyStepCompleted("UI Load", "UI Load Complete");
        }


        public override int LoadSteps
        {
            get { return 4; }
        }
    }
}
